package net;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 *
 */
public class SimpleChatClient {
    private Socket socket;
    public SimpleChatClient() throws Exception {
        // 连上服务端
        socket = new Socket("localhost",8080);
        //启动服务助手，用以接收服务返回的数据
        ServerHandler serverHandler = new ServerHandler();
        serverHandler.start();
        // 准备写出到服务端
        OutputStream os = socket.getOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(os);
        BufferedWriter bw = new BufferedWriter(osw);
        PrintWriter pw = new PrintWriter(bw,true);
        //服务端助手让他做事情

        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请输入内容");
            Scanner scan = new Scanner(System.in);
            String s = scan.nextLine();
            if ("exit".equals(s)){
                break;
            }
            pw.println(s);
        }
        pw.close();
    }

    //准备读取服务端发回来的数据
    class ServerHandler extends Thread{
        public void run() {
            try {
                //在这里读
                InputStream is = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String line = null;
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }
                br.close();
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) throws Exception {
        SimpleChatClient scc = new SimpleChatClient();
    }
}
